import React, { useState } from 'react';

const Login = (setUser, setPwd, setSname, setLogin) => {
  let param = {
    passwd: "1234567",
    username: "fdy110"
  };
  let arr = Array.from(param.username).splice(3, 3)
  let num = `${arr[0]}${arr[1]}${arr[2]}`;
  if (param.passwd !== '1234567' || num > 130 || num < 100) {
    setSname('登录失败');
    setUser(param.username);
    setPwd(param.passwd);
  }
  fetch('http://39.96.84.223:4567/verify/adminlogin', {
    //请求方式
    method: 'POST',
    //请求头
    headers: {
      'Accept': 'application/json,text/plain,*/*',/* 格式限制：json、文本、其他格式 */
      'Content-Type': 'application/json'/* 请求内容类型 */
    },
    //将请求的参数转成json
    body: JSON.stringify(param),
    // 请求的返回值
  }, []).then((response) => {
    if (response.status === 200) {
      response.json().then((data) => {
        //获取请求的返回字段
        let items = data.userinfo;
        if (items !== {}) {
          setSname('登录成功');
          setLogin(`学校是${items.university}`)
          setUser(items.username);
          setPwd('1234567')
        }
      })
    } else {
      console.log('err')
      alert("密码或账号输入有误");
    }
  }).catch((error) => {
    console.log(error)
  })
}

const App = () => {
  const [user, setUser] = useState('');
  const [pwd, setPwd] = useState('');
  const [schoolName, setSname] = useState('');
  const [loginStatus, setLogin] = useState('');

  return (
    <div>
      <input type="text" placeholder='请输入账号' value={user} type='username' />
      <input type="text" placeholder='请输入密码' value={pwd} type='password' />
      <button onClick={() => Login(setUser, setPwd, setSname, setLogin)}>登录</button>
      <h1>{loginStatus}</h1>
      <span>{schoolName}</span>
    </div>
  )
}

export default App;